
package simuri.disambiguation;

import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
import simuri.coherence.CoherenceMatrix;
import simuri.utils.Configuration;
import simuri.utils.EntityUriMap;
import simuri.utils.NamedEntity;

/**
 *
 * @author ngonga
 */
public class WeightedCoherence extends NormalizedHits{
    
      double alpha = 0.1;

    @Override
    public EntityUriMap getMapping(EntityUriMap similarities, CoherenceMatrix c,
            HashMap<String, Double> aprioriScores, Configuration config) {
        logger.info("Running weighted coherence algorithm");
        // get normalized similarities and apriori scores        
        EntityUriMap normedSims = normalize(similarities);

        logger.info("Normalized Similarities\n" + normedSims);
        EntityUriMap normedAprioriMap = normalize(getAprioriMap(normedSims, aprioriScores));

        logger.info("Normalized a-priori vector\n" + normedAprioriMap);
        //get rid of coherences between nodes set for the same entity
        c = cleanCoherenceMatrix(c, similarities);
        c.normRows();

        Set<NamedEntity> entities = normedSims.keySet();
        TreeSet<String> uris = normedSims.getAllUris();

        EntityUriMap sims = new EntityUriMap();
        double simValue;

        // stopping condition is either the maximal number of iterations is reached
        // or we reach a stable solution
        //compute similarity values
        for (NamedEntity e : normedSims.keySet()) {
            for (String uri : normedSims.get(e).keySet()) {
                simValue = 0;
                for (NamedEntity e2 : normedSims.keySet()) {
                    for (String refUri : normedSims.get(e2).keySet()) {
                        if (!e.label.equals(e2.label)) {
                            //System.out.println(e2 + ", " + refUri + " -> " + normedAprioriMap.get(e2, refUri));
                            simValue = simValue + normedSims.get(e2, refUri) * c.getCoherence(refUri, uri);
                        }
                    }
                }
                sims.add(e, uri, (1-alpha)*normedAprioriMap.get(e, uri) + alpha*simValue);
            }
        }
        sims = normalize(sims);
        
        return sims;
    }
    
}
